// ActionScript file
import mx.controls.Alert;
import mx.controls.TextArea;
import mx.controls.tabBarClasses.Tab;
import mx.events.ItemClickEvent;

import personal.gavin.gtweet.effect.GlowTween;
import personal.gavin.gtweet.event.ProcessXMLFinishEvent;
import personal.gavin.gtweet.event.StatusManagerUpdateFinishEvent;
import personal.gavin.gtweet.widget.Profile;
import personal.gavin.gtweet.widget.ProfileUtil;
import personal.gavin.gtweet.widget.Setting;
import personal.gavin.gtweet.widget.Status;
import personal.gavin.utils.ColorUtil;
import personal.gavin.utils.UIUtil;

private var beginTimer:Timer = new Timer(1500,1);
private var statusTime:Timer = new Timer(120000);
private var profileTime:Timer = new Timer(600000);
private var now_tab:int = 0;
private function init():void{
	//get parameters
	trace(this.parameters.id);//输出undefined证明获取参数失败
	update_now.visible = false;
	ps.selectedIndex = 1;
	now_tab++;
	this.beginTimer.addEventListener(TimerEvent.TIMER,initTabContent);
	this.beginTimer.addEventListener(TimerEvent.TIMER_COMPLETE,doFirstRequest);
	this.beginTimer.start();
	
	statusTime.addEventListener(TimerEvent.TIMER,requestStatus);
	profileTime.addEventListener(TimerEvent.TIMER,requestProfile);
	statusTime.start();
	profileTime.start();
}
private function getParameters():void{
	trace(this.parameters.id);
	Setting.username = this.parameters.id;
	trace("parameter over");
	//Alert.show("You are viewing "+Setting.username);
	//Setting.username = "gavinxixi";
}
private function initTabContent(event:TimerEvent):void{
	now_tab++;
	ps.selectedIndex = now_tab%2;
}
private function doFirstRequest(event:Event):void{
	this.requestStatus(event);
	this.requestProfile(event);
}
/*
 * repaint methods
 */
 private function backgroundRepaint():void{
 	//state style
	this.setStyle("backgroundColor","#"+Setting.globol_profile.profile_background_color);
	this.setStyle("backgroundGradientAlphas",[1.0, 1.0]);
	this.setStyle("backgroundGradientColors",[Setting.globol_profile.profile_background_color, Setting.globol_profile.profile_background_color]);
 	//backgrond image style
 	backgroud.setStyle("backgroundColor","#"+Setting.globol_profile.profile_background_color);
 }
 private function textRepaint():void{
 	//status color
 	updateStatus(null);
 }
 private function linkRepaint():void{
 	//nickname
 	this.nickname.setStyle("color","#" + Setting.globol_profile.profile_link_color);
 	//tabBar style
 	_tabbar.setStyle("color","#" + Setting.globol_profile.profile_link_color);
    var color_roll:String = ColorUtil.adjustBrightness2S("#"+Setting.globol_profile.profile_link_color,45);
 	trace(color_roll);
    var tab:Tab;
    for (var idx:uint = 0; idx < _tabbar.dataProvider.length; idx++) {
        tab = Tab(_tabbar.getChildAt(idx));
        tab.setStyle("textRollOverColor",color_roll);
    }
    //tag content style
	contentStyle();
 	//avatar style
	new GlowTween(avatar,ColorUtil.colorStringToInt("#"+Setting.globol_profile.profile_link_color), 2, 5);
 }
 private function sidefillRepaint():void{
 	//tabBar style
 	var color:String = "#"+ Setting.globol_profile.profile_sidebar_fill_color;
    var tab:Tab;
    for (var idx:uint = 0; idx < _tabbar.dataProvider.length; idx++) {
        tab = Tab(_tabbar.getChildAt(idx));
        tab.setStyle("fillColors", [color, color]);
        tab.setStyle("backgroundColor", color);
    }
 }
 private function sideborderRepaint():void{
 	//avatar style
	avatar.setStyle("borderColor","#"+Setting.globol_profile.profile_sidebar_border_color);
 }
 
 /*
 * tabbar event
 */
private function tabBar_creationComplete():void {
    var color:String = "#"+ Setting.globol_profile.profile_sidebar_fill_color;
    var color_roll:String = ColorUtil.adjustBrightness2S("#"+Setting.globol_profile.profile_link_color,45);
 	trace(color_roll);
    var tab:Tab;
    var idx:uint;
    var len:uint = _tabbar.dataProvider.length;
    for (idx = 0; idx < len; idx++) {
        tab = Tab(_tabbar.getChildAt(idx));
        tab.setStyle("fillColors", [color, color]);
        tab.setStyle("fillAlphas", [0.3, 0.6]);
        tab.setStyle("backgroundColor", color);
        tab.setStyle("textRollOverColor",color_roll);
    }
}        
private function tabBar_itemClick(evt:ItemClickEvent):void {
    ps.selectedIndex = evt.index;
}
private function contentStyle():void {
	for (var i:int=0; i<ps.getChildren().length ; i++){
		var content:Canvas = Canvas(ps.getChildAt(i));
		content.setStyle("backgroundColor","#"+ Setting.globol_profile.profile_sidebar_fill_color);
	}
}

/*
 * text event control panel hide and show
 */
 private function rollOverStatus(evt:MouseEvent):void {
 	trace("over");
 	trace(evt.localX + " " + evt.localY);
 	text_control.visible = true;
 }
 private function rollOutStatus(evt:MouseEvent):void {
 	trace("out");
 	trace(evt.localX + " " + evt.localY);
 	text_control.visible = false;
 }
 
 /*
 * text move
 */
private function previousStatus():void{
	if (!addStatus()){
		prev_button.enabled = false;
		return;
	}
	prev_button.enabled = false;
	next_button.enabled = false;
	move_new.target = another_status;
	move_new.xBy = content_panel.width;
	move_new.duration = 500;
	move_old.target = status_panel;
	move_old.xBy = content_panel.width;
	move_old.duration = 500;        	
	move_old.play();
	move_new.play();
	
}
private function nextStatus():void{
	if (!addStatus(false)){
		next_button.enabled = false;
		return;
	}
	next_button.enabled = false;
	prev_button.enabled = false;
	move_new.target = another_status;
	move_new.xBy = 0 - content_panel.width;
	move_new.duration = 500;
	move_old.target = status_panel;
	move_old.xBy = 0 - content_panel.width;
	move_old.duration = 500;        	
	move_old.play();
	move_new.play();
}
private function addStatus(_left:Boolean = true):Boolean{
	var temp_canvas:Canvas = new Canvas();
	var offset:int = 0;
	if (_left) {
		temp_canvas.x = 0 - content_panel.width;
		offset = -1;
	} else {
		temp_canvas.x = content_panel.width;
		offset = 1;
	}        	
	if (this.now_index + offset < 0 || this.now_index + offset > Setting.globol_statuser.getCount()-1){
		trace(this.now_index + " " + Setting.globol_statuser.getCount());
		return false;
	}
	var _status:Status = Setting.globol_statuser.getStatus(this.now_index + offset);
	
	this.now_id = _status.id;
	this.now_index = this.now_index + offset;
	controlUpdate();
	
	temp_canvas.y = 0;
	temp_canvas.width = content_panel.width;
	temp_canvas.height = content_panel.height;
	var temp_text:TextArea = new TextArea();
	temp_text.x = 0;
	temp_text.y = 0;
	temp_text.width = 210;
	temp_text.height = 99.3;
	temp_text.editable = false;
	temp_text.setStyle("borderStyle","none");
	temp_text.htmlText = UIUtil.statusToHtml(_status);
	//is it read
	if(!_status.read){
		var newBackColor:String = ColorUtil.adjustBrightness2S("#"+Setting.globol_profile.profile_background_color,85);
		temp_canvas.setStyle("backgroundColor",newBackColor);
		temp_text.setStyle("backgroundColor",newBackColor);
		_status.read = true;
	}
	temp_canvas.addChildAt(temp_text,0);
	another_status = temp_canvas;
	content_panel.addChildAt(another_status,0);
	return true;
}
private function removeOldStatus():void{
	content_panel.removeChildAt(1);
	status_panel = another_status;
	controlUpdate();
}
private function controlUpdate():void{
	if(now_index < 0 || now_index > Setting.globol_statuser.getCount()-1 || 
	now_id == -1 || Setting.globol_statuser.getCount() == 0){
		trace(this.now_index + " " + Setting.globol_statuser.getCount());
		prev_button.enabled = false;
		next_button.enabled = false;
		count.text = "0/0";
		return;
	} else if(now_index == 0){
		prev_button.enabled = false;
		if (Setting.globol_statuser.getCount()-1 == 0){
			next_button.enabled = false;
		}else {
			next_button.enabled = true;
		}
	} else if(now_index == Setting.globol_statuser.getCount()-1){
		prev_button.enabled = true;
		next_button.enabled = false;
	} else {
		prev_button.enabled = true;
		next_button.enabled = true;
	}
	count.text = this.now_index+1 + "/" +Setting.globol_statuser.getCount();
}
/*
 * sent request for new profile and status
 */
private function requestStatus(event:Event):void{
	Setting.globol_statuser.updateStatuses(updateStatus);
	loading.visible = true;
}
private function requestProfile(event:Event):void{
	var _profile_util:ProfileUtil = new ProfileUtil();
	_profile_util.getProfileByName(Setting.username,updateProfile);
}

/*
 * status and profile update event dispatch
 */
private function updateStatus(_event:StatusManagerUpdateFinishEvent):void{
	if(Setting.globol_statuser.getCount() == 0){
		now_index = -1;
		now_id = -1;
		controlUpdate();
		loading.visible = false;
		return;
	}
	var show_id:Number = -1;
	if(!Setting.globol_statuser.isInById(now_id) || this.now_id == -1 || this.now_index == -1){
		if(this.now_id != -1 && this.now_index != -1){
			removeOldStatus();
		}
		var lastIndex:int = Setting.globol_statuser.getCount()-1;
		var _status:Status = Setting.globol_statuser.getStatus(lastIndex);
		
		this.now_id = _status.id;
		this.now_index = lastIndex;
		controlUpdate();
		
		var temp_canvas:Canvas = new Canvas();
		temp_canvas.x = 0;
    	temp_canvas.y = 0;
    	temp_canvas.width = content_panel.width;
    	temp_canvas.height = content_panel.height;
    	var temp_text:TextArea = new TextArea();
    	temp_text.x = 0;
    	temp_text.y = 0;
    	temp_text.width = 210;
    	temp_text.height = 99.3;
    	temp_text.editable = false;
    	temp_text.setStyle("borderStyle","none");
    	temp_text.htmlText = UIUtil.statusToHtml(_status);
    	//is it read
    	if(!_status.read){
    		var newBackColor:String = ColorUtil.adjustBrightness2S("#"+Setting.globol_profile.profile_background_color,85);
    		temp_canvas.setStyle("backgroundColor",newBackColor);
    		temp_text.setStyle("backgroundColor",newBackColor);
    		_status.read = true;
    	}
    	temp_canvas.addChildAt(temp_text,0);
    	status_panel = temp_canvas;
    	content_panel.addChildAt(status_panel,0);
	}else {
		this.now_index = Setting.globol_statuser.getIndexById(this.now_id);
		controlUpdate();
	}
	loading.visible = false;
}
private function updateProfile(event:ProcessXMLFinishEvent):void{
	var _profile:Profile = Profile(event.data);
	var diff:Profile = ProfileUtil.diffProfile(_profile, Setting.globol_profile);
	if ( ! ProfileUtil.isNullProfile(diff) ){
		if ( _profile.error != null){
			Alert.show(_profile.error.message);
		} else {
			Setting.globol_profile = _profile;
			rePaintdiffProfile(diff);
		}
	}
	loading.visible = false;
}
private function rePaintdiffProfile(diffProfile:Profile):void{
	/**
	 * text content update
	 */
	this.nickname.text = Setting.globol_profile.name;
	this.profile_panel.htmlText = UIUtil.profileToHtml(Setting.globol_profile);
	this.friends.text = "推友"+Setting.globol_profile.friends_count;
	this.follows.text = "关注"+Setting.globol_profile.followers_count;
	this.tweets.text = "推推"+Setting.globol_profile.statuses_count;
	/**
	 * images
	 */	
	if(diffProfile.profile_background_image_url != ""){
		this.avatar_image.load(Setting.globol_profile.profile_image_url);
	}
	if(diffProfile.profile_image_url != ""){
		this.backgroud_image.load(Setting.globol_profile.profile_background_image_url);
	}
	
	/**
	 * theme and color by offical 
	 */		
	if(diffProfile.profile_background_color != null){
		this.backgroundRepaint();
	}
	if(diffProfile.profile_text_color != null){
		this.textRepaint();
	}
	if(diffProfile.profile_link_color != null){
		this.linkRepaint();
	}
	if(diffProfile.profile_sidebar_fill_color != null){
		this.sidefillRepaint();
	}
	if(diffProfile.profile_sidebar_border_color != null){
		this.sideborderRepaint();
	}
}
/*
 * loading change
 */
 private function showUpdate():void{
 	update_now.visible = true;
 }
 private function hideUpdate():void{
 	update_now.visible = false;
 }
  
private var status_panel:Canvas;
private var another_status:Canvas;
private var now_index:int = -1;
private var now_id:Number = -1;
